﻿using SDATA;
using SDLL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SABI
{
    public partial class frm0128 : Form
    {
        bool _isNew = true;
        string sql = "";
        string _GroupID;

        public frm0128()
        {
            InitializeComponent();
        }

        public frm0128(bool isNew = true, bool isView=false, string GroupID = "")
        {
            InitializeComponent();
            _isNew = isNew;
            btnSave.Visible = !isView;
            txtGroupID.Enabled = isNew;
            btnAdd.Visible = isNew;
            _isNew = isNew;
            _GroupID = GroupID;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void frm0128_Load(object sender, EventArgs e)
        {

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            btnSave.Enabled = true;
            txtGroupID.Enabled = true;
            _isNew = true;
            txtGroupID.Text = "";
            txtGroupName.Text = "";
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!CheckInput()) { return; }
            if (!_isNew)
            {
                UpdateInfo();
            }
            else
            {
                AddNew();
            }
        }

        private bool CheckInput()
        {
            if (txtGroupID.Text.Trim() == "")
            {
                DLL02.Alert("Bạn phải nhập Mã nhóm!");
                txtGroupID.Focus();
                return false;
            }
            else if (txtGroupName.Text.Trim() == "")
            {
                DLL02.Alert("Bạn phải nhập Tên nhóm!");
                txtGroupName.Focus();
                return false;
            }
            return true;
        }

        private void AddNew()
        {
            sql = "INSERT tb0070(GroupID,GroupName,Disabled,";
            sql += "CreateDate,CreateUserID,LastModifyDate,LastModifyUserID) VALUES(";
            sql += string.Format("'{0}',N'{1}','{2}',", txtGroupID.Text, txtGroupName.Text,chkDisabled.Checked?1:0);
            sql += string.Format("GETDATE(),'{0}',GETDATE(),'{0}')", FRM.UserName);
            if (DATA.ExecuteNonQuery(sql, FRM.ConnectString, false))
            {
                sql = " Insert tb0075 (DivisionID, ModuleID, GroupID, DataID, Permission,  CreateDate, CreateUserID, LastModifyUserID, LastModifyDate) ";
                sql += string.Format("  Select Distinct DivisionID,ModuleID, '{0}' , DataID,1,GetDate(), 'ADMIN',  'ADMIN', Getdate() ",txtGroupID.Text);
                sql += string.Format("  From tb0076 Where DivisionID ='{0}'", FRM.DivisionID);
                DATA.ExecuteNonQuery(sql, FRM.ConnectString, true);

                sql = string.Format("EXEC pr0093 '{0}', 'A'", txtGroupID.Text);
                DATA.ExecuteNonQuery(sql, FRM.ConnectString, true);

                SDLL.DLL02.Info("Lưu dữ liệu thành công!");
                (Application.OpenForms["frm0123"] as frm0123).loadDATA();
                btnSave.Enabled = false;
                txtGroupID.Enabled = false;
            }
            else
            {
                SDLL.DLL02.Alert("Lưu dữ liệu thất bại!");
            }
        }

        private void UpdateInfo()
        {
            sql = string.Format("UPDATE tb0070 SET GroupName =N'{0}', Disabled='{1}',", txtGroupName.Text,chkDisabled.Checked?1:0);
            sql += string.Format("LastModifyUserID='{0}', LastModifyDate=GETDATE() WHERE GroupID='{1}'", FRM.UserName, _GroupID);
            if (SDATA.DATA.ExecuteNonQuery(sql, FRM.ConnectString, false))
            {
                SDLL.DLL02.Info("Cập nhật dữ liệu thành công!");
                (Application.OpenForms["frm0123"] as frm0123).loadDATA();
                Close();
            }
            else
            {
                SDLL.DLL02.Alert("Cập nhật dữ liệu thất bại!");
            }
        }

        private void frm0128_Shown(object sender, EventArgs e)
        {
            if (!_isNew)
            {
                sql = string.Format("SELECT * FROM tb0070 WHERE GroupID='{0}'", _GroupID);
                DataTable dt = FRM.GetDataTable(sql);
                if (dt.Rows.Count < 1) { return; }
                txtGroupID.Text = dt.Rows[0]["GroupID"].ToString();
                txtGroupName.Text = dt.Rows[0]["GroupName"].ToString();
                chkDisabled.Checked = dt.Rows[0]["Disabled"].ToString()!="0"?true:false;
            }
        }



    }
}
